# -*- coding: utf-8 -*-
"""
@Time: 12/9/2018 4:12 PM
@Author: hejing
@Email: 2010jing@gmail.com
"""
from multiprocessing import Pool
import os, time, random
import pymysql
import time, requests
from concurrent.futures import ProcessPoolExecutor
import time
import random

def long_time_task(data):
	print('Run task %s (%s)...' % (os.getpid(), os.getpid()))
	start = time.time()
	data_handler(data)
	# with ProcessPoolExecutor() as excute:
	# 	excute.map(data_handler, data)
	end = time.time()
	print('Task %s runs %0.2f seconds.' % (os.getpid(), (end - start)))

# {"s_Controller_id":6, "LightPole_id":212, "Controller_id":24, "Read_time":"2017-04-06 16:03:00", "Concentrator_name":"\u6d4b\u8bd5\u96c6\u4e2d\u5668", "Concentrator_address":"", "Electric_quantity":1.55, "Electric_voltage":225.90001, "Electric_current":0.046000004, "Power":3.0, "Power_factor":0.0, "OneSwitch_status":0, "TwoSwitch_status":0, "Fault_status":1, "LightOn_duration":0, "brightness":100},

def data_handler(data):
	conn = pymysql.connect(host='localhost', user='root', password='123456', database='gwsl', charset='utf8')
	# conn = pymysql.connect(host='120.79.254.19', user='root', password='zsdx2016', database='gwsl', charset='utf8')
	cursor = conn.cursor()
	print(data[0])
	print(data[1])
	# s_Controller_id 6
	# LightPole_id 212 / 208 / 13 / 12 /11
	# Controller_id   (150) 216 / 215 / 214 / 213 / 212 / 211 / 210 / 209 / 208 / 207 / 206 / 205 / 204 / 202 / 200 / 199 / 197 / 195 / 194 / 193 / 192 / 190 / 189 / 187 / 185

	# Read_time 2017-04-06 16:03:00
	# Concentrator_name 测试集中器001
	# Concentrator_address 测试集中器地址001

	# Electric_quantity 1--43 ?
	# Electric_voltage  0~ 220~ 245?
	# Electric_current 0 ~ 0.189
	# Power 0 ~ 46
	# Power_factor 0.0
	# OneSwitch_status 0
	# TwoSwitch_status 0
	# Fault_status 1
	# LightOn_duration 0
	# brightness 0 --100






	for i in range(data[0], data[1]):
		sql = 'insert into search_controller(LightPole_id,Controller_id,Read_time, Concentrator_name, Concentrator_address, Electric_quantity,Electric_voltage,Electric_current,Power,Power_factor,OneSwitch_status,TwoSwitch_status,Fault_status,LightOn_duration,brightness) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'



		res = cursor.execute(sql, [212, 24, '2017-04-06 16:03:00','实验ControllerDemo','T8-501',])
		conn.commit()
	cursor.close()
	conn.close()

def randomDateTime():
	a1 = (2017, 1, 1, 0, 0, 0, 0, 0, 0)  # 设置开始日期时间元组（1976-01-01 00：00：00）
	a2 = (2018, 12, 31, 23, 59, 59, 0, 0, 0)  # 设置结束日期时间元组（1990-12-31 23：59：59）

	start = time.mktime(a1)  # 生成开始时间戳
	end = time.mktime(a2)  # 生成结束时间戳

	t = random.randint(start, end)  # 在开始和结束时间戳中随机取出一个
	date_touple = time.localtime(t)  # 将时间戳生成时间元组
	datetime = time.strftime("%Y-%m-%d %H:%M:%S", date_touple)  # 将时间元组转成格式化字符串（1976-05-21）
	print(datetime)
	return  datetime


if __name__ == '__main__':
	start = time.time()
	# datas = [(0, 2500000), (2500000, 5000000), (5000000, 7500000), (7500000, 10000000)]
	# datas = [(0, 250000), (250000, 500000), (500000, 750000), (750000, 1000000)]
	datas = [(0, 2500), (2500, 5000), (5000, 7500), (7500, 10000)]
	randomDateTime()
	# print('Parent process %s.' % os.getpid())
	# p = Pool(4)
	# p.map(long_time_task, [i for i in datas])
	# print('All subprocesses done.')
	# end = time.time()
	# print('Task %s runs %0.2f seconds.' % (os.getpid(), (end - start)))
